# 项目：wstest01.py
# 文件：smoke（EI）
# 作者：刘帅
# 日期：2025/5/20 14:34
# coding:utf-8
from copyreg import add_extension
from doctest import DocTestRunner
from pkgutil import walk_packages
from time import sleep
from selenium import webdriver  # 浏览器驱动核心模块
import time,random  # 时间控制、随机数生成
from selenium.webdriver import ActionChains, Keys  # 动作链和键盘操作
from selenium.webdriver.support.select import Select  # 下拉菜单处理


def get_driver():
    driver = webdriver.Edge()  #选择浏览器
    driver.implicitly_wait(5)  #等待5秒
    driver.maximize_window()  #窗口最大化
    return driver

def input(element, value):   #用法：点击、清空、填写内容
    element.click()
    element.clear()
    element.send_keys(value)

def select_random_first(element):  #应对下拉菜单，目的是随机选择一个除第一个选项【全部】外的选项
    options_count = len(Select(element).options)  #搞出选择框的选项数
    random_index = random.randint(1, options_count-1)  #索引从 1 开始(避开第一个)随机选择一个
    Select(element).select_by_index(random_index)  #选择选中的选项

if __name__ == '__main__':
    driver = get_driver()  #调用预定义的 get_driver() 函数来获取一个配置好的浏览器控制对象
    driver.get('http://localhost:8080/YunDuanBoss/login/')  #打开网页
    uname = driver.find_element('css selector','#loginForm > div > div:nth-child(1) > input')  #找到‘请输入用户名文本框’
    input(uname,'YDGL101')  #对文本框内进行input方法（点击、清空、填入内容）
    upass = driver.find_element('name','userPass')  #找到密码文本框
    input(upass,'YunDuan123')  #输入密码
    driver.find_element('css selector','#form-login > div > div > div.modal-footer > button').click()  #点击登录
    driver.find_element('css selector','#nav2 > div:nth-child(7) > div.panel-heading > a').click()  #点击人事管理模块
    driver.find_element('css selector','#list-8 > div > ul > li:nth-child(1) > a').click()  #点击员工信息
    driver.find_element('css selector','#content > div.row.con-margin.con-body-con > div.col-lg-12.col-md-12.col-xs-12.con-body-padding.text-left > div > button').click()  #点击新增功能
    sleep(3)
    area = driver.find_element('css selector','#addEmp-form > div > div:nth-child(1) > div:nth-child(1) > select')  #找到区域下拉框
    select_random_first(area)  #随机选择区域下拉框的任意选项
    apartment = driver.find_element('css selector','#addEmp-form > div > div:nth-child(1) > div:nth-child(2) > select')
    select_random_first(apartment)
    position = driver.find_element('css selector','#addEmp-form > div > div:nth-child(1) > div:nth-child(3) > input')
    input(position,'保安')
    ename = driver.find_element('css selector','#addEmp-form > div > div:nth-child(2) > div:nth-child(1) > input')
    input(ename,'张飞')
    ssex = driver.find_element('css selector','#addEmp-form > div > div:nth-child(2) > div:nth-child(2) > select')


    def select_random_option(element):  #定义新函数，因为性别这里选项中不用排除第一个选项，所以我们定义一个不排除的函数（这里使用的人工智能辅助）
        "随机选择下拉框中的任意选项（包含第一个）"
        select = Select(element)
        if (count := len(select.options)) == 0:
            raise ValueError("下拉框没有可选项")
        select.select_by_index(random.randint(0, count - 1))
        return select.first_selected_option.text


    selected_text = select_random_option(ssex)
    sleep(3)
    driver.find_element('css selector','#addEmp-form > div > div:nth-child(2) > div:nth-child(3) > input').click()
    driver.find_element('css selector','body > div:nth-child(22) > div.datetimepicker-days > table > tfoot > tr:nth-child(1) > th').click()
    phone = driver.find_element('css selector','#addEmp-form > div > div:nth-child(3) > div:nth-child(1) > input')
    input(phone,'15891403345')
    emile = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(3) > div:nth-child(2) > input')
    input(emile, '1231287@qq.com')
    QQ = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(3) > div:nth-child(3) > input')
    input(QQ, '1231287')
    education = driver.find_element('css selector','#addEmp-form > div > div:nth-child(4) > div:nth-child(1) > select')
    select_random_first(education)
    university = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(4) > div:nth-child(2) > input')
    input(university, '加里敦大学')
    sleep(3)
    major = driver.find_element('css selector','#addEmp-form > div > div:nth-child(4) > div:nth-child(3) > input')
    input(major,'军体拳')
    address = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(5) > div:nth-child(1) > input')
    input(address,'地球')
    hukou = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(5) > div:nth-child(2) > input')
    input(hukou,'农村户口')
    card_number = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(5) > div:nth-child(3) > input')
    input(card_number,'12414127649746912641')
    ID_number = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(6) > div:nth-child(1) > input')
    input(ID_number,'6132679791236713128')
    driver.find_element('css selector','#addEmp-form > div > div:nth-child(6) > div:nth-child(2) > input').click()
    driver.find_element('css selector','body > div:nth-child(23) > div.datetimepicker-days > table > tbody > tr:nth-child(1) > td:nth-child(4)').click()
    bir_type = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(6) > div:nth-child(3) > select')
    selected_text = select_random_option(bir_type)
    contacts = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(7) > div:nth-child(1) > input')
    input(contacts,'李大庄')
    cphone = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(7) > div:nth-child(2) > input')
    input(cphone,'17688663218')
    relation = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(7) > div:nth-child(3) > input')
    input(relation,'朋友')
    work_number = driver.find_element('css selector', '#addEmp-form > div > div:nth-child(8) > div > input')
    input(work_number,'YDXA202')
    sleep(5)
    driver.find_element('id','addEmpBtn').click()